package com.hwadee.mapper;

import com.hwadee.pojo.Role;
import org.apache.ibatis.annotations.*;


import java.util.List;

@Mapper
public interface RoleMapper {
    //查询所有角色信息
    @Select("SELECT id, name, description FROM role")
    List<Role> getAllRoles();
    //根据角色 ID 删除角色
    @Delete("DELETE FROM role WHERE id = #{id}")
    int deleteRoleById(@Param("id") Integer id);

    //插入新角色
    @Insert("INSERT INTO role(name, description) VALUES(#{name}, #{description})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertRole(Role role);
    //根据角色 ID 查询其拥有的权限 ID 列表
    @Select("SELECT permission_id FROM role_permission WHERE role_id = #{roleId}")
    List<Integer> getPermissionIdsByRoleId(@Param("roleId") Integer roleId);
    //根据角色 ID 修改角色名称
    @Update("UPDATE role SET name = #{name} WHERE id = #{roleId}")
    int updateRoleName(@Param("roleId") Integer roleId, @Param("name") String name);
    //删除指定角色的所有权限关联关系
    @Delete("DELETE FROM role_permission WHERE role_id = #{roleId}")
    int deleteRolePermissions(@Param("roleId") Integer roleId);
    //为角色添加一个权限
    @Insert("INSERT INTO role_permission(role_id, permission_id) VALUES (#{roleId}, #{permissionId})")
    int addRolePermission(@Param("roleId") Integer roleId, @Param("permissionId") Integer permissionId);


}
